//确保jQuery比common.js先加载
if (typeof jQuery === "undefined") {
    throw new Error("common.js依赖于jQuery");
}

window.Common = {
    getJson: function (url, data, success, async) {
        $.ajax({
            method: "GET",
            url: url,
            cache: false,
            dataType: "json",
            data: data,
            async: typeof(async) == 'boolean' && async == false ? false : true,
            success: function (response) {
                success(response);
            }
        });
    },
    postJson: function (url, data, success, async) {
        $.ajax({
            method: "POST",
            url: url,
            cache: false,
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(data),
            async: typeof(async) == 'boolean' && async == false ? false : true,
            success: function (response) {
                success(response);
            }
        });
    },
    getUrlParam: function (key) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURIComponent(r[2]);

        return null;
    },
    // <param name="rows">树结构数据</param>
    // <param name="attributes">属性信息</param>
    convertTreeData: function (rows, attributes) {
        var keyNodes = {}, parentKeyNodes = {};
        parentKeyNodes[attributes.rootParentId] = null;

        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];

            if (!row)
                continue;

            var node = {};
            node.id = row[attributes.keyField];
            node.parentId = row[attributes.parentKeyField];
            node.text = row[attributes.textField];
            node.children = null;

            keyNodes[node.id] = node;

            if (parentKeyNodes[node.parentId]) {
                parentKeyNodes[node.parentId].push(node);
            }
            else {
                parentKeyNodes[node.parentId] = [node];
            }

            var children = parentKeyNodes[node.id];
            if (children) {
                node.children = children;
            }

            var pNode = keyNodes[node.parentId];
            if (pNode) {
                if (pNode.children == null)
                    pNode.children = [];

                pNode.children.push(node);
            }
        }

        return parentKeyNodes[attributes.rootParentId];
    },
    // <param name="rows">树结构数据</param>
    // <param name="childNodeId">要查询的子节点ID</param>
    // <param name="rootNodeId">最顶层根节点ID</param>
    // <param name="attributes">属性信息</param>
    getTreeParentNodes: function (rows, childNodeId, rootNodeId, attributes) {
        var parentNodes = [];
        var parentId = childNodeId;
        var finished = false;

        for (var i = 0; i < rows.length; i++) {
            if (finished)
                break;

            for (var j = 0; j < rows.length; j++) {
                var treeNode = rows[j];
                if (treeNode && treeNode[attributes.keyField] == parentId) {
                    parentNodes.unshift(treeNode);
                    parentId = treeNode[attributes.parentKeyField];
                    if (parentId == rootNodeId)
                        finished = true;

                    break;
                }
            }
        }

        return parentNodes;
    },
    // <param name="rows">树结构数据</param>
    // <param name="childNodeId">要查询的子节点ID</param>
    // <param name="rootNodeId">最顶层根节点ID</param>
    // <param name="attributes">属性信息</param>
    getTreeParentIdList: function (rows, childNodeId, rootNodeId, attributes) {
        var parentIdList = [];

        var parentNodes = this.getTreeParentNodes(rows, childNodeId, rootNodeId, attributes);
        for (var i = 0; i < parentNodes.length; i++) {
            var parentNode = parentNodes[i];
            parentIdList.push(parentNode[attributes.keyField]);
        }

        return parentIdList;
    },
    isEmail: function (email) {
        var emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
        return emailReg.test(email);
    },
    //根据字典类型获取数据字典列表
    getDictListByType: function (type, callback) {
        this.getJson('/sys/param/getParamListByType', {type: type},
            function (response) {
                if (!response || response.status != 200)
                    return;

                callback(response.result);
            });
    },
    //获取数据集数据
    getDataSetData: function (datasetName, params, callback) {
        this.postJson('/sys/dataset/getDataSetData?datasetName=' + datasetName,
            params,
            function (response) {
                if (!response || response.status != 200)
                    return;

                callback(response.result);
            });
    },
}